<?php
/**
 * AuctionExecuters controller class
 *
 */
class AuctionExecutersController extends Admin_Controller
{
    
    private $table_cities;
    
    public function init()
    {
    	parent::init();
        $this->_table = new Admin_Table_Auction_AuctionExecuters();   
        $this->table_cities = new Admin_Table_Auction_Cities();
        $this->setActiveCat(986);
    }

    public function indexAction()
    {
    	$this->page_type = 'list';

    	$reset = $this->_request->getParam('reset', 0);

    	$form = new Llib_Form($this->_request);
    	$data = array();

        if ($this->_request->isPost())
    	{
	    	$data['by_name'] = $this->_getParam('is_active', -1);
	    	$data['by_license'] = $this->_getParam('search_text', '');
	    	$data['id_region'] = $this->_getParam('id_region', -1);
	    	$data['id_district'] = $this->_getParam('id_district', -1);
	    	$data['id_city'] = $this->_getParam('id_city', -1);
    	}

        $data = $this->SessionVars($data, array('by_name', 'by_license', 'id_region', 'id_city', 'id_district'), $reset);


        if (isset($this->_user->koatuu)) $params['koatuu']=$this->_user->koatuu;
        
    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array('checkboxes' => true, 'link' => "/" . $this->controller . "/" )); 
    	$lclm->AddColumn('Назва', 'name', array('width' => '55%', 'sort' => 1));
    	$lclm->AddColumn('№ Ліцензії', 'license_code', array('width' => '25%', 'sort' => 1));
    	//$lclm->AddColumn('Дата дії ліцензії', 'license_due_date', array('width' => '15%', 'sort' => 1));

    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count));
        
        $id_region = intval(@$data['id_region']);
        $id_district = intval(@$data['id_district']);
        $id_city = intval(@$data['id_city']);
        
        $data['id_region'] = $this->table_cities->GetRegionsList($id_region);
        $data['id_district'] = $this->table_cities->GetDistrictsList($id_district, $id_region);
        $data['id_city'] = $this->table_cities->GetCitiesList($id_city, $id_region, $id_district);
        
        $form->AddData($data);

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('List', $lclm->GetList());
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	$this->view->xmlWriter->endElement();
    }
    
    public function editAction()
    {
        
//       echo "<script>alert('editAction');</script>";
//       print_r($this->_table);
//       exit();
        
        
    	$id = $this->_request->getParam('id');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$saveexit = $this->_getParam('saveexit');
    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost()) 
        {
		$d = $form->GetPostedData();
        	if($this->_table->update($d, $id, $this->_user->id)){ 
        		if(!is_null($saveexit)) $this->_redirect("/" . $this->controller);
                $form->SetSuccess();
        	}else $form->SetError($this->_table->GetError());        	
        }
        
        $data = $this->_table->GetDataRow($id);

        if(empty($data['activity'])) unset($data['activity']);
        $id_region = intval(@$data['id_region']);
        $id_district = intval(@$data['id_district']);
        $id_city = intval(@$data['id_city']);
        
        $data['id_region'] = $this->table_cities->GetRegionsList($id_region);
        $data['id_district'] = $this->table_cities->GetDistrictsList($id_district, $id_region);
        $data['id_city'] = $this->table_cities->GetCitiesList($id_city, $id_region, $id_district);        
        
        $form->AddData($data);       

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('Title', 'Редагувати');
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }    

    public function removeAction()
    {
    	$form = new Llib_Form($this->_request);
    	$d = $form->GetPostedData();
    	if(count($d['selected_id']) > 0) $this->_table->remove($d['selected_id'], $this->_user->id);
    	$this->_redirect('/'.$this->controller);    	
    }

}
